home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 1 / Macwelt DVD 1.toast / Web-Publishing / HTML-Editoren / Alpha ƒ / Tcl / Modes / HTML and CSS Modes / htmlcssInit.tcl < prev    next >
Encoding:
Text File  |  2001-01-12  |  15.7 KB  |  425 lines

  1. ## -*-Tcl-*-
  2.  # ###################################################################
  3.  #  HTML and CSS mode - tools for editing HTML and CSS
  4.  # 
  5.  #  FILE: "htmlcssInit.tcl"
  6.  #                                    created: 99-07-15 21.54.38 
  7.  #                                last update: 01-01-12 18.22.06 
  8.  #  Author: Johan Linde
  9.  #  E-mail: <alpha_www_tools@go.to>
  10.  #     www: <http://go.to/alpha_www_tools>
  11.  #  
  12.  # Version: 3.0 and 2.0
  13.  # 
  14.  # Copyright 1996-2001 by Johan Linde
  15.  #  
  16.  # This program is free software; you can redistribute it and/or modify
  17.  # it under the terms of the GNU General Public License as published by
  18.  # the Free Software Foundation; either version 2 of the License, or
  19.  # (at your option) any later version.
  20.  # 
  21.  # This program is distributed in the hope that it will be useful,
  22.  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24.  # GNU General Public License for more details.
  25.  # 
  26.  # You should have received a copy of the GNU General Public License
  27.  # along with this program; if not, write to the Free Software
  28.  # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29.  # 
  30.  # ###################################################################
  31.  ##
  32.  
  33. #===============================================================================
  34. # This file initialises a bunch of things, much of which are used in both CSS
  35. # and HTML modes.
  36. #===============================================================================
  37.  
  38. proc htmlcssInit.tcl {} {}
  39.  
  40. # HTML mode version
  41. set htmlVersion 3.0
  42. set html::PrefsFolder [file join $PREFS HTML]
  43. set html::TmpFolder [file join $PREFS HTMLtmp]
  44.  
  45. #===============================================================================
  46. # ◊◊◊◊ Update preferences ◊◊◊◊ #
  47. #===============================================================================
  48.  
  49. if {![info exists html::PrefsVersion] || ${html::PrefsVersion} < $htmlVersion} {
  50.     catch {htmlPrefsUpdater.tcl}
  51. }
  52.  
  53. #===============================================================================
  54. # ◊◊◊◊ Menu icons ◊◊◊◊ #
  55. #===============================================================================
  56.  
  57. set htmlIconTxt {"Netscape Navigator 3" "Netscape Navigator 4" "Netscape Communicator" "Internet Explorer" Cyberdog iCab MacLynx Mosaic}
  58. set htmlIcons {•135 •293 •294 •295 •281 •297 •296 •942}
  59.  
  60. # Menu icons
  61. newPref v htmlMenuIcon •294 HTML
  62. newPref v htmlUtilsMenuIcon •281 HTML
  63.  
  64. if {$HTMLmodeVars(htmlMenuIcon) == $HTMLmodeVars(htmlUtilsMenuIcon)} {
  65.     foreach _tmp $htmlIcons {
  66.         if {$HTMLmodeVars(htmlMenuIcon) != $_tmp} {
  67.             set HTMLmodeVars(htmlUtilsMenuIcon) $_tmp
  68.             prefs::modifiedModeVar htmlUtilsMenuIcon HTML
  69.             break
  70.         }
  71.     }
  72. }
  73.  
  74. set htmlMenu $HTMLmodeVars(htmlMenuIcon)
  75. set htmlUtilsMenu $HTMLmodeVars(htmlUtilsMenuIcon)
  76.  
  77. #===============================================================================
  78. # ◊◊◊◊ Mode variables ◊◊◊◊ #
  79. #===============================================================================
  80.  
  81. # Line width
  82. newPref v fillColumn 75 HTML
  83. newPref v leftFillColumn 0 HTML
  84. # word breaking and word wrapping
  85. newPref v wordBreak {\w+} HTML
  86. newPref v wordBreakPreface {(\W)} HTML
  87. newPref v wrapBreak {[\w_]+} HTML
  88. newPref v wrapBreakPreface {([^\w_])} HTML
  89. newPref f wordWrap    1 HTML
  90.  
  91. # The content of these elements is indented
  92. newPref v indentElements {DIR DL MENU OL TABLE TR UL} HTML
  93.  
  94. # browsers
  95. if {![info exists browserSig] && [catch {getFileSig [icGetPref -t 1 Helper•http]} browserSig]} {set browserSig MOSS}
  96. # Browser signatures
  97. newPref v browsers {MOSS MSIE} HTML
  98.  
  99. newPref v prefixString    "<!-- " HTML
  100. newPref v suffixString    " -->" HTML
  101.  
  102. # Template folders.
  103. newPref v templateFolders {} HTML
  104. # Tag color
  105. newPref v tagColor blue HTML
  106. # Attribute color
  107. newPref v attributeColor magenta HTML
  108. # Simple coloring?
  109. newPref f simpleColoring 0 HTML
  110. # Always color immediately when typing
  111. newPref f ColorImmediately 0 HTML
  112. # Update META DATE?
  113. newPref f updateMetaDate 0 HTML
  114. # Should elements be lower case?
  115. newPref f useLowerCase    0 HTML
  116. # Should •'s be inserted?
  117. newPref    f useTabMarks    1 HTML
  118. # Use the optional closing tag of these elements
  119. newPref v optionalClosing {P TR TD TH} HTML
  120. # A window cache with frames.
  121. newPref v windows {} HTML
  122. # A list of content types
  123. newPref v contenttypes {text/css text/javascript multipart/form-data} HTML
  124. # A list of link types
  125. newPref v linktypes {stylesheet alternate start next prev contents index glossary copyright 
  126. chapter section subsection appendix help bookmark} HTML
  127. # When browser is launched, should it be brought to front?
  128. newPref    f browseInForeground    1 HTML
  129. # Save without asking when sending file to browser?
  130. newPref f saveWithoutAsking 0 HTML
  131. # List of commonly used character entities
  132. newPref v commonChars {"less than" "greater than" "ampersand" "nonbreak space"} HTML
  133. # Never ask about extensions?
  134. newPref f hideExtensions 0 HTML
  135. # Never ask about deprecated elements and attributes?
  136. newPref f hideDeprecated 0 HTML
  137. # Attributes globally not asked about at first
  138. newPref v dontaskforAttributes {} HTML
  139. # Attributes globally never asked about
  140. newPref v neveraskforAttributes {} HTML
  141. # Attributes globally always asked about
  142. newPref v alwaysaskforAttributes {} HTML
  143. # Beep when asking for attributes in the status bar?
  144. newPref f promptNoisily 1 HTML
  145. # Flash status bar for first attribute?
  146. newPref f flashStatusBar 1 HTML
  147. # Input from dialog windows or status bar?
  148. newPref f useBigWindows 1 HTML
  149. # Change attributes in dialog windows or status bar?
  150. newPref f changeInBigWindows 1 HTML
  151. # Create new file if missing without asking when cmd-double-clicking a link.
  152. newPref f createWithoutAsking 0 HTML
  153. # Cmd-double-click on non text file link opens file?
  154. newPref f openNonTextFile 1 HTML
  155. # Return on non text file in home page window opens file?
  156. newPref f homeOpenNonTextFile 1 HTML
  157. # Check anchors in links?
  158. newPref f checkAnchors 1 HTML
  159. # Case sensistive link checking?
  160. newPref f caseSensitive 0 HTML
  161. # Check links with Big Brother?
  162. newPref f useBigBrother 0 HTML
  163. newPref f checkInFront 1 HTML
  164. newPref f useBBoptions 1 HTML
  165. newPref f ignoreRemote 0 HTML
  166. newPref f ignoreLocal 0 HTML
  167. # Folder for HTML manual.
  168. newPref v manualFolder [file join $HOME "HTML mode manual"] HTML
  169. newPref v manualStartPage 0 HTML
  170. # FTP servers
  171. newPref v FTPservers {} HTML
  172. # Last modified string
  173. newPref v lastModified "Last modified" HTML
  174. # 'Insert include tags' only inserts tags, and not the file?
  175. newPref f includeOnlyTags 1 HTML
  176. # Color JavaScript keywords?
  177. newPref f JavaScriptColoring 0 HTML
  178. # Color of JavaScript keywords
  179. newPref v JavaScriptColor    magenta HTML
  180. # Color of strings
  181. newPref v stringColor green HTML
  182. # Color of JavaScript comments
  183. newPref v JavaCommentColor red HTML
  184. # Color CSS keywords?
  185. newPref f CSSColoring 0 HTML
  186. # Color of CSS keywords
  187. newPref v CSSColor cyan HTML
  188. # A list of URLs, cached, to pick from for insertion
  189. newPref v URLs {} HTML
  190. # Home pages.
  191. newPref v homePages {} HTML
  192. # Media types
  193. newPref v mediatypes {aural braille embossed handheld print projection screen tty tv all} HTML
  194. # Active color sets
  195. newPref v activeColorSets {} HTML
  196. # Explain types prefs window.
  197. newPref f explainTypePrefs 1 HTML
  198. # Auto-indent when typing >
  199. newPref f electricGreater 1 HTML
  200. # Warn before mirroring with Anarchie
  201. newPref f anarchieMirrorWarn 1 HTML
  202.  
  203. #===============================================================================
  204. # ◊◊◊◊ Other variables ◊◊◊◊ #
  205. #===============================================================================
  206.  
  207. # These elements have optional closing tags
  208. set html::OptionalClosingTags {COLGROUP DD DT LI OPTION P THEAD TBODY TFOOT TR TH TD}
  209. # These elements can be in document HEAD.
  210. set html::HeadElements {BASE ISINDEX LINK META STYLE SCRIPT OBJECT}
  211. # These elements are plug-ins.
  212. set html::Plugins {EMBED}
  213. # These elements do not appear in the strict DTD
  214. set html::NotInStrict {APPLET FONT CENTER DIR MENU STRIKE S U BASEFONT ISINDEX NOFRAMES IFRAME FRAMESET FRAME}
  215. # These elements are deprecated
  216. set html::DeprecatedElems {APPLET FONT CENTER DIR MENU STRIKE S U BASEFONT ISINDEX}
  217. # These elements do not appear in transitional DTD
  218. set html::NotInTransitional {FRAME FRAMESET}
  219. # These elements are extensions to HTML
  220. set html::HTMLextensions {BGSOUND BLINK EMBED ILAYER KEYGEN LAYER MARQUEE MULTICOL NOBR NOEMBED NOLAYER SERVER SPACER WBR}
  221. # Formatting styles
  222. lappend html::formattingStyles NO-FORMATTING C-STYLE-FORMATTING
  223. set html::indentLineProcs(NO-FORMATTING) {}
  224. set html::formatBlockProcs(NO-FORMATTING) {}
  225. set html::indentLineProcs(C-STYLE-FORMATTING) {::indentLine}
  226. set html::formatBlockProcs(C-STYLE-FORMATTING) {::indentRegion}
  227.  
  228. foreach __vvv [array names HTMLmodeVars] {
  229.     set HTMLmodeVarsInvisible($__vvv) 1
  230. }
  231.  
  232. unset __vvv
  233.  
  234. # Temporary fix!
  235. if {![alpha::package vsatisfies ${alpha::tclversion} 7.4b8]} {
  236.     ;proc HTMLmodifyFlags {} {
  237.         html::modifyFlags
  238.     }
  239.     ;proc CSSmodifyFlags {} {
  240.         css::modifyFlags
  241.     }
  242.     ;proc dialog::specialView::file {f} {
  243.         dialog::specialView_file $f
  244.     }
  245. }
  246.  
  247. #
  248. # Internal Globals
  249. #
  250. # Watch out! Large comments crashes Alpha!!!
  251. set css::CommentRegexp {/\*[^*]*\*+([^/][^*]*\*+)*/}
  252. set html::CommentRegexp {<!--([^-]|-[^-])*-->}
  253. set htmlCurSel    ""
  254. set htmlIsSel    0
  255. set htmlNumBbthChecking 0
  256. set html::HideDeprecated 0
  257. set html::HideExtensions 0
  258. set html::HideFrames 0
  259.  
  260. #===============================================================================
  261. # ◊◊◊◊ Colors for background, text, links etc. ◊◊◊◊ #
  262. #===============================================================================
  263.  
  264. # Predefined colors
  265. set css::Colors {ActiveBorder ActiveCaption AppWorkspace Background ButtonFace ButtonHighlight
  266. ButtonShadow ButtonText CaptionText GrayText Highlight HighlightText InactiveBorder
  267. InactiveCaption InactiveCaptionText InfoBackground InfoText Menu MenuText Scrollbar
  268. ThreeDDarkShadow ThreeDFace ThreeDHighLight ThreeDLightShadow ThreeDShadow Window
  269. WindowFrame WindowText}
  270.  
  271. proc html::NewColor {var val } {
  272.     global html::ColorName
  273.     global html::ColorNumber
  274.     set html::ColorName($var) $val 
  275.     set html::ColorNumber($val) $var
  276. }
  277. html::NewColor black    "#000000"
  278. html::NewColor silver    "#C0C0C0"
  279. html::NewColor gray        "#808080"
  280. html::NewColor white    "#FFFFFF"
  281. html::NewColor maroon    "#800000"
  282. html::NewColor red        "#FF0000"
  283. html::NewColor purple    "#800080"
  284. html::NewColor fuchsia    "#FF00FF"
  285. html::NewColor green    "#008000"
  286. html::NewColor lime        "#00FF00"
  287. html::NewColor olive    "#808000"
  288. html::NewColor yellow    "#FFFF00"
  289. html::NewColor navy        "#000080"
  290. html::NewColor blue        "#0000FF"
  291. html::NewColor teal        "#008080"
  292. html::NewColor aqua        "#00FFFF"
  293.  
  294. # A list of colours
  295. set html::basicColors [lsort [array names html::ColorName]]
  296. rename html::NewColor ""
  297. html::ReadActiveColorSets
  298.  
  299. #===============================================================================
  300. # ◊◊◊◊ Cmd-Double-click ◊◊◊◊ #
  301. #===============================================================================
  302.  
  303. proc HTML::DblClick {from to} {
  304.     global mode file::separator
  305.     global HTMLmodeVars filepats
  306.     
  307.     # Build regular expressions with URL attrs.
  308.     if {$mode == "HTML"} {
  309.         set exp [html::URLregexp]
  310.     }
  311.  
  312.     set expcss {(url)\([ \t\r\n]*("[^"]+"|'[^']+'|[^ "'\t\n\r\)]+)[ \t\r\n]*\)}
  313.     # Check if user clicked on a link.
  314.     if {($mode == "HTML" && ![catch {search -s -f 0 -r 1 -i 1 -m 0 $exp $from} res] && [pos::compare [lindex $res 1] > $from]) ||
  315.     (![set curl [catch {search -s -f 0 -r 1 -i 1 -m 0 $expcss $from} res]] && [pos::compare [lindex $res 1] > $from])} {
  316.         # Get path to this window.
  317.         if {![string length [set thisURL [html::ThisFilePath 1]]]} {return}
  318.         # Get path to link.
  319.         if {[info exists curl]} {set exp $expcss}
  320.         regexp -nocase $exp [eval getText $res] dum1 dum2 linkTo
  321.         set linkTo [html::URLunEscape [string trim $linkTo "\"' \t\r\n"]]
  322.         # Anchors points to file itself if no BASE. (No BASE if [llength $thisURL] > 4)
  323.         if {[string index $linkTo 0] == "#" && [llength $thisURL] > 4} {
  324.             set a [string range $linkTo 1 end]
  325.             if {![catch {search -s -f 1 -r 1 -i 1 -m 0 \
  326.                 "<(A|MAP)\[ \t\r\n\]+\[^>\]*NAME\[ \t\r\n\]*=\[ \t\r\n\]*(\"\[ \t\r\n\]*$a\[ \t\r\n\]*\"|'\[ \t\r\n\]*$a\[ \t\r\n\]*'|$a)(>|\[ \t\r\n\]+\[^<>\]*>)" [minPos]} anc]} {
  327.                 goto [lindex $anc 0]
  328.                 insertToTop
  329.             }
  330.             return
  331.         }
  332.         if {[catch {lindex [html::PathToFile [lindex $thisURL 0] [lindex $thisURL 1] [lindex $thisURL 2] [lindex $thisURL 3] $linkTo] 0} linkToPath]} {
  333.             if {$linkToPath == ""} {
  334.                 message "Link not well-defined."
  335.             } else {
  336.                 message "Link points to $linkToPath. Doesn't map to a file on the disk."
  337.             }
  338.             return
  339.         }
  340.         # Does the file exist? 
  341.         if {[file exists $linkToPath] && ![file isdirectory $linkToPath]} {
  342.             # Is it a text file?
  343.             if {[getFileType $linkToPath] == "TEXT"} {
  344.                 edit -c $linkToPath
  345.                 if {[regexp {[^#]*#(.+)$} $linkTo dum anchor] && ![catch {search -s -f 1 -r 1 -i 1 -m 0 \
  346.                   "<(A|MAP)\[ \t\r\n\]+\[^>\]*NAME\[ \t\r\n\]*=\[ \t\r\n\]*(\"\[ \t\r\n\]*$anchor\[ \t\r\n\]*\"|'\[ \t\r\n\]*$anchor\[ \t\r\n\]*'|$anchor)(>|\[ \t\r\n\]+\[^<>\]*>)" [minPos]} anc]} {
  347.                     goto [lindex $anc 0]
  348.                     insertToTop
  349.                 }
  350.             } elseif {[set HTMLmodeVars(openNonTextFile)] && [getFileType $linkToPath] != "APPL"} {
  351.                 launchDoc $linkToPath
  352.             } else {
  353.                 message "[file tail $linkToPath] is not a text file."
  354.             }
  355.         } else {
  356.             set isAnHtmlFile 0
  357.             set sufficies ""
  358.             foreach mm {HTML CSS JScr} {
  359.                 if {[info exists filepats($mm)]} {set sufficies [concat $sufficies $filepats($mm)]}
  360.             }
  361.             foreach suffix $sufficies {
  362.                 if {[string match $suffix $linkToPath]} {set isAnHtmlFile 1}
  363.             }
  364.             if {(![file exists $linkToPath] && !$isAnHtmlFile) || [file isdirectory $linkToPath] ||
  365.             ![regexp "\[^${file::separator}\]+" $linkToPath disk] || ![file exists $disk${file::separator}]} {
  366.                 message "Cannot open [file tail $linkToPath]."
  367.             } else {
  368.                 set htmlFile [file tail $linkToPath]
  369.                 if {![set HTMLmodeVars(createWithoutAsking)]} {
  370.                     set dval [dialog -w 350 -h 160 -t "The file '$htmlFile' does not exist.\
  371.                       Do you want to open a new empty window with this name?\
  372.                       It will automatically be saved in the right place,\
  373.                       and if necessary, new folders will be created."  10 10 340 100 \
  374.                       -c "Create missing file without asking in the future." [set HTMLmodeVars(createWithoutAsking)] 10 105 340 120 \
  375.                       -b Yes 20 130 85 150 -b No 115 130 180 150]
  376.                     if {[lindex $dval 2]} {return}
  377.                     if {[lindex $dval 0] != [set HTMLmodeVars(createWithoutAsking)]} {
  378.                         set HTMLmodeVars(createWithoutAsking) [lindex $dval 0]
  379.                         prefs::modifiedModeVar createWithoutAsking HTML
  380.                     }
  381.                 }
  382.                 # Create a new file and open it.
  383.                 foreach p [split [file dirname $linkToPath] ${file::separator}] {
  384.                     append path "$p${file::separator}"
  385.                     # make new folders if needed.
  386.                     if {![file exists $path]} {
  387.                         file mkdir $path
  388.                     } elseif {![file isdirectory $path]} {
  389.                         alertnote "Cannot make a new folder '[file tail [file dirname $path]]'.\
  390.                         There is already a file with the same name."
  391.                         return
  392.                     }
  393.                 }
  394.                 append path "$htmlFile"
  395.                 # create an empty file.
  396.                 set fid [open $path w]
  397.                 # I suppose it's best to close it, too.
  398.                 close $fid
  399.                 edit $path
  400.             }
  401.         }
  402.     } elseif {$mode == "HTML"} { 
  403.         if {![catch {search -s -f 0 -r 1 -i 1 -m 0 {(FILE|PATH|INCLPATH)=\"[^\"]+\"} $from} res] && [pos::compare [lindex $res 1] > $from]} {
  404.             regexp -nocase {(FILE|PATH|INCLPATH)=\"[^\"]+\"} [eval getText $res] fil
  405.             set fil [html::ResolveInclPath $fil [html::WhichInclFolder [set win [html::StrippedFrontWindowPath]]] [file dirname $win]]
  406.             if {[file exists $fil]} {
  407.                 edit -c $fil
  408.             } else {
  409.                 beep
  410.                 message "File not found."
  411.             }
  412.         } elseif {[html::IsInContainer SCRIPT]} {
  413.             JScr::DblClick $from $to
  414.         } elseif {[html::IsInContainer STYLE]} {
  415.             global css::IsDescriptor
  416.             if {![catch {search -s -f 0 -r 1 -m 0 {[ \t\r\n]+[^ \t\r\n;:]+[ \t\r\n]*:} $from} res] && [pos::compare [lindex $res 1] >= $to]} {
  417.                 set css::IsDescriptor [css::IsInAtRule font-face]
  418.                 css::Dialog [string tolower [string trim [eval getText $res] " \t\r\n:"]]
  419.             }
  420.         } elseif {![html::RevealColor 1]} {
  421.             html::EditTag 2
  422.         }
  423.     }
  424. }
  425.